<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { GetData } from '@/api/modules/memberscoreexchange'

// const imgDomain = 'https://img1.eprhan.cc'

const findFrom = ref({
  OrderNo: '',
  UserId: '',
  SCOpenId: '',
  Flag: '',
  Name: '',
  Phone: '',
  Time: [],
})
const dataList: any = ref([])
const loading = ref(true)
const total = ref(0)
let PageSize = 20
const activeNames = ref([])

// 获取数据
function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.OrderNo && ({ OrderNo: findFrom.value.OrderNo })),
    ...(findFrom.value.UserId && ({ UserId: findFrom.value.UserId })),
    ...(findFrom.value.SCOpenId && ({ SCOpenId: findFrom.value.SCOpenId })),
    ...(findFrom.value.Flag !== '' && ({ Flag: findFrom.value.Flag })),
    ...(findFrom.value.Name && ({ Name: findFrom.value.Name })),
    ...(findFrom.value.Phone && ({ Phone: findFrom.value.Phone })),
    ...(findFrom.value.Time.length !== 0 && ({ StartAddTime: findFrom.value.Time[0], EndAddTime: findFrom.value.Time[1] })),
  }
  GetData(params).then((res: any) => {
    if (res.statusCode === 200) {
      dataList.value = res.data.items
      total.value = res.data.total
      let index = 0
      activeNames.value = dataList.value.reduce((acc: any) => {
        acc[index] = ['1']
        index++
        return acc
      }, {})
      loading.value = false
    }
    else {
      ElMessage.error('获取数据失败')
      loading.value = false
    }
  })
}

// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}

function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
}

onMounted(() => {
  GetDataList()
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="订单编号">
                <el-input v-model="findFrom.OrderNo" placeholder="请输入订单编号" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="用户ID">
                <el-input v-model="findFrom.UserId" placeholder="请输入用户ID" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="用户OpenId">
                <el-input v-model="findFrom.SCOpenId" placeholder="请输入用户OpenId" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="兑换状态">
                <el-select v-model="findFrom.Flag" placeholder="请选择" style="width: 240px;">
                  <el-option label="正常" :value="1" />
                  <el-option label="待支付积分" :value="-10" />
                  <el-option label="已退单" :value="-11" />
                </el-select>
              </el-form-item>
              <el-form-item label="姓名">
                <el-input v-model="findFrom.Name" placeholder="请输入姓名" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="手机号">
                <el-input v-model="findFrom.Phone" placeholder="请输入手机号" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="时间">
                <el-date-picker
                  v-model="findFrom.Time"
                  clearable
                  type="datetimerange"
                  range-separator="到"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  @keyup.enter="Inquire"
                />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <!-- <el-table v-loading="loading" :data="dataList" stripe style="width: 100%;" border>
        <el-table-column align="center" width="100" prop="id" label="ID" />
        <el-table-column align="center" prop="subscriber" label="用户信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.subscriber">
              未授权
            </div>
            <div v-else class="user-name">
              <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.subscriber?.userAvatar" />
              <div>
                <p style="margin: 0;">
                  {{ scope.row.subscriber?.userNickName }}
                </p>
                <span>
                  {{ scope.row.subscriber?.userPhone }}
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" width="300" prop="subscriber.scOpenId" label="用户OpenID" />
        <el-table-column align="center" width="300" prop="orderNo" label="订单编号" />
        <el-table-column align="center" width="140" prop="flag" label="兑换状态">
          <template #default="scope">
            <el-tag>
              {{ scope.row.flag === 1 ? '正常'
                : scope.row.flag === -10 ? '待支付积分'
                  : scope.row.flag === -11 ? '已退单' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" width="140" prop="productId" label="商品ID" />
        <el-table-column align="center" width="140" prop="productName" label="商品名称" />
        <el-table-column align="center" width="140" prop="salePrice" label="销售价" />
        <el-table-column align="center" width="140" prop="saleScore" label="销售积分价" />
        <el-table-column align="center" width="140" prop="count" label="兑换数量" />
        <el-table-column align="center" width="140" prop="totalScore" label="总计积分" />
        <el-table-column align="center" width="140" prop="name" label="姓名" />
        <el-table-column align="center" width="140" prop="phone" label="电话" />
        <el-table-column align="center" width="140" prop="province" label="省" />
        <el-table-column align="center" width="140" prop="city" label="市" />
        <el-table-column align="center" width="140" prop="area" label="区" />
        <el-table-column header-align="center" width="180" prop="address" label="地址" />
        <el-table-column align="center" width="140" prop="addtime" label="兑换时间" />
        <el-table-column align="center" width="140" prop="updatetime" label="最后更新时间" />
      </el-table> -->
      <el-card v-for="(item, index) in dataList" :key="item.orderNum" class="order">
        <template #header>
          <div class="card-header">
            <span>订单编号：{{ item.orderNo }}</span>
          </div>
        </template>
        <!-- 基本信息 -->
        <el-collapse v-model="activeNames[index]">
          <el-collapse-item title="用户信息" name="1">
            <el-descriptions border>
              <el-descriptions-item label="用户头像" :rowspan="2">
                <el-avatar shape="square" :size="60" :src="item.subscriber?.userAvatar" />
              </el-descriptions-item>
              <el-descriptions-item label="用户ID">
                {{ item.subscriber?.userId }}
              </el-descriptions-item>
              <el-descriptions-item label="用户昵称">
                {{ item.subscriber?.userNickName }}
              </el-descriptions-item>
              <el-descriptions-item label="用户授权手机号">
                {{ item.subscriber?.userPhone }}
              </el-descriptions-item>
              <el-descriptions-item label="收货地区">
                {{ item.province }}-{{ item.city }}-{{ item.area }}
              </el-descriptions-item>
              <el-descriptions-item label="详细地址">
                {{ item.address }}
              </el-descriptions-item>
              <el-descriptions-item label="姓名">
                {{ item.name }}
              </el-descriptions-item>
              <el-descriptions-item label="电话">
                {{ item.phone }}
              </el-descriptions-item>
            </el-descriptions>
          </el-collapse-item>
          <el-collapse-item title="订单信息" name="2">
            <el-descriptions border>
              <el-descriptions-item label="商品ID">
                {{ item.productId }}
              </el-descriptions-item>
              <el-descriptions-item label="商品名称">
                {{ item.productName }}
              </el-descriptions-item>
              <el-descriptions-item label="销售价">
                {{ item.salePrice }}
              </el-descriptions-item>
              <el-descriptions-item label="销售积分价">
                {{ item.saleScore }}
              </el-descriptions-item>
              <el-descriptions-item label="兑换状态">
                {{ item.flag === 1 ? '正常'
                  : item.flag === -10 ? '待支付积分'
                    : item.flag === -11 ? '已退单' : '未定义' }}
              </el-descriptions-item>
              <el-descriptions-item label="兑换数量">
                {{ item.count }}
              </el-descriptions-item>
              <el-descriptions-item label="总计积分">
                {{ item.totalScore }}
              </el-descriptions-item>
              <el-descriptions-item label="兑换时间">
                {{ item.addtime }}
              </el-descriptions-item>
              <el-descriptions-item label="最后更新时间">
                {{ item.updatetime }}
              </el-descriptions-item>
            </el-descriptions>
          </el-collapse-item>
        </el-collapse>
      </el-card>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;
  margin-right: 25px;

  div {
    text-align: left;
  }
}

.order {
  margin-bottom: 20px;
}
</style>
